package com.frxuexi.repair.view

import android.widget.SearchView.OnQueryTextListener
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.frxuexi.lib_base.R
import com.frxuexi.lib_base.entity.RepairBeanItem
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.RequestType
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.lib_base.utils.ArouterManager
import com.frxuexi.repair.RepairIntent
import com.frxuexi.repair.RepairViewModel
import com.frxuexi.repair.adapter.RepairAdapter
import com.frxuexi.repair.databinding.ActivityRepairBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = "/repair/RepairActivity")
@AndroidEntryPoint
class RepairActivity : BaseActivity<ActivityRepairBinding, RepairIntent, RepairViewModel>() {
    override val binding: ActivityRepairBinding by lazy {
        ActivityRepairBinding.inflate(
            layoutInflater
        )
    }
    override val vm: RepairViewModel by viewModels<RepairViewModel>()
    val repairAdapter by lazy { RepairAdapter() }
    val list = mutableListOf<RepairBeanItem>()


    var flag = false//升序
    var content = ""
    var position=-1
    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect {
                when (it) {
                    is UiState.onSuccess<*> -> {
                        when(it.type){
                            RequestType.DEFAULT->{
                                val items = it.data as Collection<RepairBeanItem>
                                list.clear()
                                list.addAll(items)
                                repairAdapter.apply {
                                    data.clear()
                                    addData(items.sortedByDescending { it.id })
                                    notifyDataSetChanged()
                                }
                                if (items.size == 0) {
                                    vm.sendIntent(RepairIntent.getRepairsByNum(content))
                                }
                            }
                            RequestType.ITEM->{
                                val item = it.data as RepairBeanItem
                                list.clear()
                                list.add(item)
                                repairAdapter.apply {
                                    data.clear()
                                    addData(item)
                                    notifyDataSetChanged()
                                }
                            }
                            RequestType.DELETE->{
//                                ToastUtils.showLong("删除成功")
                                repairAdapter.data.removeAt(position)
                                repairAdapter.notifyItemRemoved(position)
                            }

                            else -> {}
                        }
                    }

                    else -> {}
                }
            }
        }

    }

    override fun initView() {
        binding.repairRv.apply {
            adapter = repairAdapter
            layoutManager = LinearLayoutManager(this@RepairActivity)
        }

        binding.repairOrderCon.setOnClickListener {
            repairAdapter.apply {
                data.clear()
                if (flag) {
                    addData(list.sortedByDescending { it.repairTime })
                    binding.repairOrder.setImageResource(R.drawable.ic_action_down)
                } else {
                    addData(list.sortedBy { it.repairTime })
                    binding.repairOrder.setImageResource(R.drawable.ic_action_asc)
                }
                notifyDataSetChanged()
            }
            flag = !flag
        }

        binding.repairSv.setOnQueryTextListener(object : OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                content = query!!
                vm.sendIntent(RepairIntent.getRepairsByUname(content))
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if (newText.isNullOrEmpty()) {
                    vm.sendIntent(RepairIntent.getRepairs)
                }
                return true
            }
        })

        repairAdapter.setOnItemLongClickListener { adapter, view, position ->
            this.position=position
            vm.sendIntent(RepairIntent.deleteRepair(repairAdapter.data.get(position).id!!))
            return@setOnItemLongClickListener true
        }

        binding.repairBtn.setOnClickListener {
            ArouterManager.nav(this,"/repair/RepairEditActivity")
        }

        repairAdapter.setOnItemClickListener { adapter, view, position ->
            ArouterManager.nav(this,"/repair/RepairApproveActivity",repairAdapter.data.get(position).id!!)
        }

        repairAdapter.addChildClickViewIds(com.frxuexi.repair.R.id.repair_update)
        repairAdapter.setOnItemChildClickListener { adapter, view, position ->
            ArouterManager.nav(this,"/repair/RepairEditActivity",repairAdapter.data.get(position).id!!)
        }



    }

    override fun onResume() {
        super.onResume()
        vm.sendIntent(RepairIntent.getRepairs)
    }

}